1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module soup.Auth;
26 
27 private import glib.ConstructionException;
28 private import glib.ListSG;
29 private import glib.Str;
30 private import glib.c.functions;
31 private import gobject.ObjectG;
32 private import soup.Message;
33 private import soup.URI;
34 private import soup.c.functions;
35 public  import soup.c.types;
36 
37 
38 /**
39  * The abstract base class for handling authentication. Specific HTTP
40  * Authentication mechanisms are implemented by its subclasses, but
41  * applications never need to be aware of the specific subclasses
42  * being used.
43  */
44 public class Auth : ObjectG
45 {
46 	/** the main Gtk struct */
47 	protected SoupAuth* soupAuth;
48 
49 	/** Get the main Gtk struct */
50 	public SoupAuth* getAuthStruct(bool transferOwnership = false)
51 	{
52 		if (transferOwnership)
53 			ownedRef = false;
54 		return soupAuth;
55 	}
56 
57 	/** the main Gtk struct as a void* */
58 	protected override void* getStruct()
59 	{
60 		return cast(void*)soupAuth;
61 	}
62 
63 	/**
64 	 * Sets our main struct and passes it to the parent class.
65 	 */
66 	public this (SoupAuth* soupAuth, bool ownedRef = false)
67 	{
68 		this.soupAuth = soupAuth;
69 		super(cast(GObject*)soupAuth, ownedRef);
70 	}
71 
72 
73 	/** */
74 	public static GType getType()
75 	{
76 		return soup_auth_get_type();
77 	}
78 
79 	/**
80 	 * Creates a new #SoupAuth of type @type with the information from
81 	 * @msg and @auth_header.
82 	 *
83 	 * This is called by #SoupSession; you will normally not create auths
84 	 * yourself.
85 	 *
86 	 * Params:
87 	 *     type = the type of auth to create (a subtype of #SoupAuth)
88 	 *     msg = the #SoupMessage the auth is being created for
89 	 *     authHeader = the WWW-Authenticate/Proxy-Authenticate header
90 	 *
91 	 * Returns: the new #SoupAuth, or %NULL if it could
92 	 *     not be created
93 	 *
94 	 * Throws: ConstructionException GTK+ fails to create the object.
95 	 */
96 	public this(GType type, Message msg, string authHeader)
97 	{
98 		auto __p = soup_auth_new(type, (msg is null) ? null : msg.getMessageStruct(), Str.toStringz(authHeader));
99 
100 		if(__p is null)
101 		{
102 			throw new ConstructionException("null returned by new");
103 		}
104 
105 		this(cast(SoupAuth*) __p, true);
106 	}
107 
108 	/**
109 	 * Call this on an auth to authenticate it; normally this will cause
110 	 * the auth's message to be requeued with the new authentication info.
111 	 *
112 	 * Params:
113 	 *     username = the username provided by the user or client
114 	 *     password = the password provided by the user or client
115 	 */
116 	public void authenticate(string username, string password)
117 	{
118 		soup_auth_authenticate(soupAuth, Str.toStringz(username), Str.toStringz(password));
119 	}
120 
121 	/**
122 	 * Tests if @auth is able to authenticate by providing credentials to the
123 	 * soup_auth_authenticate().
124 	 *
125 	 * Returns: %TRUE if @auth is able to accept credentials.
126 	 *
127 	 * Since: 2.54
128 	 */
129 	public bool canAuthenticate()
130 	{
131 		return soup_auth_can_authenticate(soupAuth) != 0;
132 	}
133 
134 	/**
135 	 * Frees @space.
136 	 *
137 	 * Params:
138 	 *     space = the return value from soup_auth_get_protection_space()
139 	 */
140 	public void freeProtectionSpace(ListSG space)
141 	{
142 		soup_auth_free_protection_space(soupAuth, (space is null) ? null : space.getListSGStruct());
143 	}
144 
145 	/**
146 	 * Generates an appropriate "Authorization" header for @msg. (The
147 	 * session will only call this if soup_auth_is_authenticated()
148 	 * returned %TRUE.)
149 	 *
150 	 * Params:
151 	 *     msg = the #SoupMessage to be authorized
152 	 *
153 	 * Returns: the "Authorization" header, which must be freed.
154 	 */
155 	public string getAuthorization(Message msg)
156 	{
157 		auto retStr = soup_auth_get_authorization(soupAuth, (msg is null) ? null : msg.getMessageStruct());
158 
159 		scope(exit) Str.freeString(retStr);
160 		return Str.toString(retStr);
161 	}
162 
163 	/**
164 	 * Returns the host that @auth is associated with.
165 	 *
166 	 * Returns: the hostname
167 	 */
168 	public string getHost()
169 	{
170 		return Str.toString(soup_auth_get_host(soupAuth));
171 	}
172 
173 	/**
174 	 * Gets an opaque identifier for @auth, for use as a hash key or the
175 	 * like. #SoupAuth objects from the same server with the same
176 	 * identifier refer to the same authentication domain (eg, the URLs
177 	 * associated with them take the same usernames and passwords).
178 	 *
179 	 * Returns: the identifier
180 	 */
181 	public string getInfo()
182 	{
183 		auto retStr = soup_auth_get_info(soupAuth);
184 
185 		scope(exit) Str.freeString(retStr);
186 		return Str.toString(retStr);
187 	}
188 
189 	/**
190 	 * Returns a list of paths on the server which @auth extends over.
191 	 * (All subdirectories of these paths are also assumed to be part
192 	 * of @auth's protection space, unless otherwise discovered not to
193 	 * be.)
194 	 *
195 	 * Params:
196 	 *     sourceUri = the URI of the request that @auth was generated in
197 	 *         response to.
198 	 *
199 	 * Returns: the list of
200 	 *     paths, which can be freed with soup_auth_free_protection_space().
201 	 */
202 	public ListSG getProtectionSpace(URI sourceUri)
203 	{
204 		auto __p = soup_auth_get_protection_space(soupAuth, (sourceUri is null) ? null : sourceUri.getURIStruct());
205 
206 		if(__p is null)
207 		{
208 			return null;
209 		}
210 
211 		return new ListSG(cast(GSList*) __p, true);
212 	}
213 
214 	/**
215 	 * Returns @auth's realm. This is an identifier that distinguishes
216 	 * separate authentication spaces on a given server, and may be some
217 	 * string that is meaningful to the user. (Although it is probably not
218 	 * localized.)
219 	 *
220 	 * Returns: the realm name
221 	 */
222 	public string getRealm()
223 	{
224 		return Str.toString(soup_auth_get_realm(soupAuth));
225 	}
226 
227 	/** */
228 	public string getSavedPassword(string user)
229 	{
230 		return Str.toString(soup_auth_get_saved_password(soupAuth, Str.toStringz(user)));
231 	}
232 
233 	/** */
234 	public ListSG getSavedUsers()
235 	{
236 		auto __p = soup_auth_get_saved_users(soupAuth);
237 
238 		if(__p is null)
239 		{
240 			return null;
241 		}
242 
243 		return new ListSG(cast(GSList*) __p, true);
244 	}
245 
246 	/**
247 	 * Returns @auth's scheme name. (Eg, "Basic", "Digest", or "NTLM")
248 	 *
249 	 * Returns: the scheme name
250 	 */
251 	public string getSchemeName()
252 	{
253 		return Str.toString(soup_auth_get_scheme_name(soupAuth));
254 	}
255 
256 	/** */
257 	public void hasSavedPassword(string username, string password)
258 	{
259 		soup_auth_has_saved_password(soupAuth, Str.toStringz(username), Str.toStringz(password));
260 	}
261 
262 	/**
263 	 * Tests if @auth has been given a username and password
264 	 *
265 	 * Returns: %TRUE if @auth has been given a username and password
266 	 */
267 	public bool isAuthenticated()
268 	{
269 		return soup_auth_is_authenticated(soupAuth) != 0;
270 	}
271 
272 	/**
273 	 * Tests whether or not @auth is associated with a proxy server rather
274 	 * than an "origin" server.
275 	 *
276 	 * Returns: %TRUE or %FALSE
277 	 */
278 	public bool isForProxy()
279 	{
280 		return soup_auth_is_for_proxy(soupAuth) != 0;
281 	}
282 
283 	/**
284 	 * Tests if @auth is ready to make a request for @msg with. For most
285 	 * auths, this is equivalent to soup_auth_is_authenticated(), but for
286 	 * some auth types (eg, NTLM), the auth may be sendable (eg, as an
287 	 * authentication request) even before it is authenticated.
288 	 *
289 	 * Params:
290 	 *     msg = a #SoupMessage
291 	 *
292 	 * Returns: %TRUE if @auth is ready to make a request with.
293 	 *
294 	 * Since: 2.42
295 	 */
296 	public bool isReady(Message msg)
297 	{
298 		return soup_auth_is_ready(soupAuth, (msg is null) ? null : msg.getMessageStruct()) != 0;
299 	}
300 
301 	/** */
302 	public void savePassword(string username, string password)
303 	{
304 		soup_auth_save_password(soupAuth, Str.toStringz(username), Str.toStringz(password));
305 	}
306 
307 	/**
308 	 * Updates @auth with the information from @msg and @auth_header,
309 	 * possibly un-authenticating it. As with soup_auth_new(), this is
310 	 * normally only used by #SoupSession.
311 	 *
312 	 * Params:
313 	 *     msg = the #SoupMessage @auth is being updated for
314 	 *     authHeader = the WWW-Authenticate/Proxy-Authenticate header
315 	 *
316 	 * Returns: %TRUE if @auth is still a valid (but potentially
317 	 *     unauthenticated) #SoupAuth. %FALSE if something about @auth_params
318 	 *     could not be parsed or incorporated into @auth at all.
319 	 */
320 	public bool update(Message msg, string authHeader)
321 	{
322 		return soup_auth_update(soupAuth, (msg is null) ? null : msg.getMessageStruct(), Str.toStringz(authHeader)) != 0;
323 	}
324 }